package com.pai4j.pgc.controller.community;
import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.config.annotation.RequestPermission;
import com.pai4j.common.config.annotation.SafeClick;
import com.pai4j.common.config.annotation.ValidateLogin;
import com.pai4j.common.enums.ClickEvent;
import com.pai4j.common.exception.BusinessException;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.domain.community.CTCHomeRecommendQuestionVO;
import com.pai4j.domain.community.vo.request.CCQuestionSaveRequestVO;
import com.pai4j.domain.community.vo.response.cc.CTCQuestionBaseVO;
import com.pai4j.domain.community.vo.response.cc.CTCQuestionDetailVO;
import com.pai4j.domain.community.vo.response.cc.CTCQuestionListVO;
import com.pai4j.pgc.service.community.CCQuestionService;
import com.pai4j.pgc.service.community.CTCScheduleService;
import com.pai4j.remote.pgc.community.CCQuestionClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  技术社区问答
 *
 * @author: CYM-pai
 * @date: 2025/10/26 21:32
 **/
@RestController
public class CCQuestionController implements CCQuestionClient {

    @Autowired
    private CCQuestionService questionService;
    @Autowired
    private CTCScheduleService scheduleService;

    @Override
    @ValidateLogin
    @SafeClick(event = ClickEvent.SUBMIT, time = 5000)
    public PAIResponseBean<Long> save(@RequestBody CCQuestionSaveRequestVO questionSaveRequest) {
        if (questionSaveRequest.getTitle().contains("xbs")) {
            throw new BusinessException("检测到非法链接");
        }
        questionSaveRequest.validate();
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(questionService.save(questionSaveRequest, userId));
    }

    /**
     * 设置置顶
     * @param id
     * @param status
     * @return
     */
    @Override
    @RequestPermission
    public PAIResponseBean<Boolean> setTop(@PathVariable Long id, @PathVariable Boolean status) {
        String userId = SessionHelper.getCurrentUserId();
        questionService.setTop(id, status, userId);
        return PAIResponseBeanUtil.success(true);
    }

    /**
     * 删除问题
     * @param id
     * @return
     */
    @Override
    public PAIResponseBean<Boolean> delete(@PathVariable Long id) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success( questionService.delete(id, userId));
    }

    /**
     * 设置推荐
     * @param id
     * @param status
     * @return
     */
    @Override
    @RequestPermission
    public PAIResponseBean<Boolean> setRecommend(@PathVariable Long id, @PathVariable Boolean status) {
        String userId = SessionHelper.getCurrentUserId();
        questionService.setRecommend(id, status, userId);
        return PAIResponseBeanUtil.success(true);
    }

    /**
     * 问题详情
     * @param questionId
     * @return
     */
    @Override
    public PAIResponseBean<CTCQuestionDetailVO> detail(@PathVariable Long questionId) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(questionService.getPermissionQuestionDetailFromCache(questionId, userId));
    }

    @Override
    @RequestPermission
    public PAIResponseBean<Boolean> updateFree(@PathVariable Long id) {
        String userId = SessionHelper.getCurrentUserId();
        questionService.updateFree(id, userId);
        return PAIResponseBeanUtil.success(true);
    }

    @Override
    @ValidateLogin
    public PAIResponseBean<CTCQuestionBaseVO> detailBase(@PathVariable Long questionId) {
        return PAIResponseBeanUtil.success(questionService.getQuestionBaseFromCache(questionId));
    }

    @Override
    public PAIPageResponseBeanUtil<List<CTCQuestionListVO>> pageList(@PathVariable String listType,
                                                                        @PathVariable Long pathId,
                                                                        @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        return questionService.pageList(listType, pathId, pageNo, pageSize);
    }

    /**
     * 管理问题列表查询
     *
     * @param pathId
     * @param kw
     * @param top
     * @param recommend
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    @RequestPermission
    public PAIPageResponseBeanUtil<CTCQuestionListVO> adminPageList(
            @RequestParam(value = "pathId", required = false) Long pathId,
            @RequestParam(value = "kw", required = false) String kw,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "top", required = false) Boolean top,
            @RequestParam(value = "recommend", required = false) Boolean recommend,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return questionService.adminPageList(pathId, kw, top, recommend, status, pageNo, pageSize);
    }


    @Override
    public PAIResponseBean<List<CTCHomeRecommendQuestionVO>> recommend(@PathVariable Long pathId) {

        return PAIResponseBeanUtil.success(scheduleService.getHomeQuestionFromCache(pathId));
    }

     @Override
    public PAIResponseBean<List<CTCHomeRecommendQuestionVO>> newList(@PathVariable Long pathId, @PathVariable Integer size) {
        return PAIResponseBeanUtil.success(questionService.getNewListFromCache(pathId, size));
    }
}
